/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.model.item.etcitem.action;

import com.lineage.configure.Config;
import com.lineage.server.PacketCreate;
import com.lineage.server.datatables.MagicDollTable;
import com.lineage.server.datatables.NpcTable;
import com.lineage.server.model.Instance.L1DollInstance;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.item.ItemExecutor;
import com.lineage.server.packetserver.S_OwnCharStatus;
import com.lineage.server.packetserver.S_ServerMessage;
import com.lineage.server.packetserver.S_SkillIconGFX;
import com.lineage.server.packetserver.S_SkillSound;
import com.lineage.server.templates.L1MagicDoll;
import com.lineage.server.templates.L1Npc;

/**
 * Class <code>MagicDoll</code> 魔法娃娃.
 * 
 * @author jrwz
 * @version 2012-6-23上午09:41:40
 * @see com.lineage.server.model.item.etcitem.action
 * @since JDK1.7
 */
public final class MagicDoll extends ItemExecutor {

    /**
     * 取得实例.
     * 
     * @return 实例
     */
    public static ItemExecutor get() {
        return new MagicDoll();
    }

    /**
     * 使用魔法娃娃.
     * 
     * @param pc
     *            使用者
     * @param itemId
     *            道具ID
     * @param itemObjectId
     *            道具OBJID
     */
    private static void useMagicDoll(final L1PcInstance pc, final int itemId,
            final int itemObjectId) {

        final L1MagicDoll magic_doll = MagicDollTable.getInstance()
                .getTemplate((itemId));

        if (magic_doll != null) {
            boolean isAppear = true;
            L1DollInstance doll = null;

            for (final L1DollInstance curdoll : pc.getDollList().values()) {
                doll = curdoll;
                if (doll.getItemObjId() == itemObjectId) {
                    isAppear = false;
                    break;
                }
            }

            if (isAppear) {
                if (!pc.getInventory().checkItem(41246, 50)) {
                    pc.sendPackets(new S_ServerMessage(337, "$5240")); // 魔法结晶体不足
                    return;
                }

                if (pc.getDollList().size() >= Config.MAX_DOLL_COUNT) {
                    // \f1没有任何事情发生。
                    PacketCreate.get().sendSinglePackets(pc, "没有任何事情发生");
                    return;
                }

                final int npcId = magic_doll.getDollId();

                final L1Npc template = NpcTable.getInstance()
                        .getTemplate(npcId);
                doll = new L1DollInstance(template, pc, itemId, itemObjectId);
                pc.sendPackets(new S_SkillSound(doll.getId(), 5935));
                pc.broadcastPacket(new S_SkillSound(doll.getId(), 5935));
                pc.sendPackets(new S_SkillIconGFX(56, 1800));
                pc.sendPackets(new S_OwnCharStatus(pc));
                pc.getInventory().consumeItem(41246, 50);
            } else {
                pc.sendPackets(new S_SkillSound(doll.getId(), 5936));
                pc.broadcastPacket(new S_SkillSound(doll.getId(), 5936));
                doll.deleteDoll();
                pc.sendPackets(new S_SkillIconGFX(56, 0));
                pc.sendPackets(new S_OwnCharStatus(pc));
            }
        }
    }

    /** 魔法娃娃. */
    private MagicDoll() {
    }

    @Override
    public void execute(final L1PcInstance pc, final L1ItemInstance useItem,
            final int readC1, final int readC2, final int targetId,
            final int targetX, final int targetY) {
        // 取得道具ID
        final int itemId = useItem.getItemId();
        final int itemObjid = useItem.getId();
        useMagicDoll(pc, itemId, itemObjid);
    }
}
